Page({
    data: {
      board: [],
      selected: null,
      images: ['1', '2', '3', '4', '5', '6', '7'],
      gridSize: 56, // 8 * 7
      boardWidth: 0,
      boardHeight: 0,
      isPaused: false,
      isGameOver: false,
      score: 0,
      minutes: 13,
      seconds: 0,
      milliseconds: 0,
      intervalId: null,
      finishTime: null  /*显示结束时间*/

    },
  
    onLoad: function () {
      this.initBoard();
      if (!this.data.isSuccess) {
        this.startTimer();
      }
    },
  
    initBoard: function () {
      const { gridSize, images } = this.data;
      const board = new Array(gridSize).fill(null);
  
      for (let i = 0; i < gridSize / 2; i++) {
        let index1, index2;
        do {
          index1 = Math.floor(Math.random() * gridSize);
          index2 = Math.floor(Math.random() * gridSize);
        } while (board[index1] !== null || board[index2] !== null || index1 === index2);
  
        board[index1] = images[i % images.length];
        board[index2] = images[i % images.length];
      }
  
      this.setData({
        board: board,
        boardWidth: 7 * (this.data.cellSize + this.data.cellPadding) - this.data.cellPadding,
        boardHeight: 8 * (this.data.cellSize + this.data.cellPadding) - this.data.cellPadding
      });
    },
  
    startTimer: function () {
      const that = this;
      that.setData({
        intervalId: setInterval(function () {
          if (that.data.isPaused || that.data.isGameOver || that.data.isSuccess) return;
  
          let { minutes, seconds, milliseconds } = that.data;
  
          milliseconds -= 10; // 减少10毫秒（因为每100毫秒调用一次）
          if (milliseconds < 0) {
            milliseconds = 90;
            seconds--;
          }
          if (seconds < 0) {
            seconds = 59;
            minutes--;
          }
          if (minutes < 0 && seconds < 0) {
            clearInterval(that.data.intervalId);
            that.setData({
              intervalId: null,
              isSuccess: true // 当时间结束时设置成功状态
            });
            wx.showToast({
              title: '时间到！',
              icon: 'none'
            });
            return;
          }
  
          that.setData({
            milliseconds: milliseconds.toString().padStart(2, '0'),
            seconds: seconds.toString().padStart(2, '0'),
            minutes: minutes.toString().padStart(2, '0')
          });
        }, 100) // 每100毫秒更新一次
      });
    },
  
    onUnload: function () {
      if (this.data.intervalId) {
        clearInterval(this.data.intervalId);
      }
    },
  
    togglePause: function () {
      this.setData({
        isPaused: !this.data.isPaused
      });
    },
  
    restartGame: function () {
      console.log('Restarting game...');
      this.setData({
        minutes: 13,
        seconds: 0,
        milliseconds: 0, // 重置毫秒
        intervalId: null,
        isPaused: false,
        isGameOver: false,
        score: 0,
        board: []
      });
      this.initBoard();
      this.startTimer();
    },
  
    stopTimer: function () {
      clearInterval(this.data.intervalId);
    },
  
    onCellTap: function (event) {
      if (this.data.isPaused || this.data.isGameOver) return;
  
      const index = event.currentTarget.dataset.index;
      const { selected, board } = this.data;
  
      if (selected === null) {
        this.setData({ selected: index });
      } else if (selected !== index && board[selected] === board[index]) {
        // 创建一个新数组来避免直接修改原始数据
        let newBoard = [...board];
        board[selected] = null;
        board[index] = null;

        this.setData({
          board: board,
          selected: null,
          score: this.data.score + 1
        });
        if (board.every(cell => cell === null)) {
          this.stopTimer();
          this.setData({ isGameOver: true });
          // 在此处添加页面跳转逻辑
          wx.navigateTo({
              url: '/pages/second/second'
            });
        }
      } else {
        this.setData({ selected: null });
      }
    },
  
    pauseGame: function () {
      this.setData({ isPaused: true });
    },
  
    resumeGame: function () {
      this.setData({ isPaused: false });
    },

/*设置结束时间点*/
    onLoad: function (options) {
        // 初始化数据
      },
    
      onFinish: function () {
        // 记录完成时间
        const finishTime = new Date();
        this.setData({ finishTime });
    
        // 跳转到 second 页面，并传递完成时间
        wx.navigateTo({
          url: '../second/second?finishTime=' + finishTime.getTime()
        });
      }

  });